package com.yugao.fintech.antelope.auth.assembly;

import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.alibaba.fastjson2.JSONObject;
import com.yugao.fintech.antelope.auth.model.ThirdBindContent;
import com.yugao.fintech.antelope.auth.model.rqrs.third.*;
import com.yugao.fintech.antelope.common.core.BaseAssembly;
import com.yugao.fintech.antelope.base.model.constants.SexEnum;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.zhyd.oauth.enums.AuthUserGender;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthUser;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.Named;

/**
 * oauth客户端
 */
@Mapper(componentModel = "spring")
public interface ThirdAuthAssembly extends BaseAssembly {
  .

  @Mappings({
          @Mapping(source = "authCode", target = "auth_code"),
          @Mapping(source = "authorizationCode", target = "authorization_code"),
          @Mapping(source = "oauthToken", target = "oauth_token"),
          @Mapping(source = "oauthVerifier", target = "oauth_verifier"),
  })
  AuthCallback toAuthCallback(ThirdLoginReq source);

  @Mappings({
          @Mapping(source = "uuid", target = "openId"),
          @Mapping(source = "gender", target = "sex", qualifiedByName = "toSex"),
          @Mapping(source = "rawUserInfo", target = "rawUserInfo", qualifiedByName = "toJSONObject2"),
  })
  ThirdAuthUser toThirdAuthUser(AuthUser source);

  ThirdAuthUser toThirdAuthUser(WxMaUserInfo wxMaUserInfo);

  LoginQrcodeTokenResp toLoginQrcodeTokenResp(ThirdAuthUser thirdAuthUser);


  ThirdBindContent toThirdBindContent(ThirdFirstBindReq source);


  @Named("toJSONObject2")
  default JSONObject toJSONObject2(com.alibaba.fastjson.JSONObject jsonObject) {
    return new JSONObject(jsonObject);
  }

  @Named("toSex")
  default String toSex(AuthUserGender gender) {
    if (gender == AuthUserGender.FEMALE) {
      return SexEnum.FEMALE.getCode();
    } else if (gender == AuthUserGender.MALE) {
      return SexEnum.MALE.getCode();
    } else {
      return SexEnum.UNKNOWN.getCode();
    }
  }

  WxJsapiSignatureResp toWxJsapiSignatureResp(WxJsapiSignature source);
}
